from django.shortcuts import render
from django.shortcuts import render
from django.http import HttpResponse

from myadmin.models import Users,Types,Goods
from django.contrib.auth.hashers import make_password, check_password
# Create your views here.


# 获取导航分类
def gettype():
    
    # 获取所有的二级分类 
    data = Types.objects.exclude(pid=0)[:6]
    return data

# 首页
def index(request):
    # 获取所有的顶级分类
    data = Types.objects.filter(pid=0)

    for x in data:
        x.sub = Types.objects.filter(pid=x.id)
        for v in x.sub:
            v.sub = Goods.objects.filter(typeid=v.id)[:6]

    context = {'typelist':gettype(),'navlist':data}
    return render(request,'home/index.html',context)    


# 列表页
def list(request,tid):
    if tid== '0':
        data = Goods.objects.all()
    else:
        data= Goods.objects.filter(typeid=tid)

    # data = Goods.objects.all()
    from django.utils.html import format_html
    for x in data:
        txt =  format_html(x.info)
        x.info  = txt

    context = {'typelist':gettype(),'alist':data}
    return render(request,'home/list.html',context)    

# 详情页
def info(request,tid):

    data= Goods.objects.get(id=tid)
    from django.utils.html import format_html
    data.info =  format_html(data.info)


    context = {'typelist':gettype(),'binfo':data}
    return render(request,'home/info.html',context)    


# 注册
def register(request):
    # 判断当前请求方式
    if request.method == 'GET':
        return render(request,'home/register.html')
    elif request.method == 'POST':
        # 执行注册
        # 判断用户名是否存在
        res = Users.objects.filter(username=request.POST['username']).exists()
        if res :
            return HttpResponse('<script>alert("用户名已存在");location.href="/register"</script>')
        else:

            ob = Users()
            ob.username = request.POST['username']
            ob.password = make_password(request.POST['password'], None, 'pbkdf2_sha256')
            ob.save()
            # 把用户信息存储到session
            request.session['VipUser'] = {'name':ob.username,'pic':ob.picurl,'uid':ob.id}

            return HttpResponse('<script>alert("注册成功,欢迎登录");location.href="/"</script>')


# 登录
def login(request):
    if request.method == 'GET':
        return render(request,'home/login.html')
    elif request.method == 'POST':
        # 查询用户名
        # 判断验证码是否正确
        if request.POST['code'].lower() != request.session['verifycode'].lower():
            return HttpResponse('<script>alert("验证码错误");location.href="/login/"</script>')
        # 判断用户密码
        ob = Users.objects.filter(username=request.POST['username'])
        if ob:
            ob = ob[0]
            # 判断密码
            if check_password(request.POST['password'],ob.password):
                # 密码正确
                # 进行登录
                request.session['VipUser'] = {'name':ob.username,'pic':ob.picurl,'uid':ob.id}

                return HttpResponse('<script>alert("登录成功");location.href="/"</script>')

        return HttpResponse('<script>alert("用户名或密码错误");location.href="/login/"</script>')


# 退出登录
def loginout(request):
    
    del request.session['VipUser']

    return HttpResponse('<script>location.href="/"</script>')

